package service

import (
	"Data-API-MCP/backend/internal/domain/entity"
	"Data-API-MCP/backend/internal/domain/repository"
	"context"
)

// DBService 数据库服务接口
type DBService interface {
	// 连接管理
	CreateConnection(ctx context.Context, conn *entity.DBConnection) error
	GetConnections(ctx context.Context, page, pageSize int, keyword string) ([]*entity.DBConnection, int64, error)
	GetConnection(ctx context.Context, id int64) (*entity.DBConnection, error)
	UpdateConnection(ctx context.Context, conn *entity.DBConnection) error
	DeleteConnection(ctx context.Context, id int64) error
	TestConnection(ctx context.Context, conn *entity.DBConnection) error

	// 表管理
	GetTables(ctx context.Context, id int64) ([]*entity.TableInfo, error)
	GetTableInfo(ctx context.Context, id int64, tableName string) (*entity.TableInfo, error)
	GetColumns(ctx context.Context, id int64, tableName string) ([]*entity.ColumnInfo, error)
	GetTableData(ctx context.Context, id int64, tableName string, page, pageSize int) (*entity.QueryResult, error)

	// 查询执行
	ExecuteQuery(ctx context.Context, id int64, query string) (*entity.QueryResult, error)
	ExecuteTransaction(ctx context.Context, id int64, queries []string) error
	ExecuteBatch(ctx context.Context, id int64, query string, params [][]interface{}) error

	// 数据库管理
	CreateDatabase(ctx context.Context, id int64, name string) error
	DropDatabase(ctx context.Context, id int64, name string) error
	CreateTable(ctx context.Context, id int64, tableName string, columns []*entity.ColumnInfo) error
	DropTable(ctx context.Context, id int64, tableName string) error
	AlterTable(ctx context.Context, id int64, tableName string, changes []string) error

	// 监控统计
	GetDatabaseStats(ctx context.Context, id int64) (*entity.DatabaseStats, error)
	GetTableStats(ctx context.Context, id int64, tableName string) (*entity.TableStats, error)
	GetQueryStats(ctx context.Context, id int64) ([]*entity.QueryStats, error)
	GetConnectionStats(ctx context.Context, id int64) (*entity.ConnectionStats, error)
}

// DBServiceImpl 数据库服务实现
type DBServiceImpl struct {
	repo repository.DBRepository
}

// NewDBService 创建数据库服务实例
func NewDBService(repo repository.DBRepository) DBService {
	return &DBServiceImpl{
		repo: repo,
	}
}

// CreateConnection 创建数据库连接
func (s *DBServiceImpl) CreateConnection(ctx context.Context, conn *entity.DBConnection) error {
	return s.repo.CreateConnection(ctx, conn)
}

// GetConnections 获取数据库连接列表
func (s *DBServiceImpl) GetConnections(ctx context.Context, page, pageSize int, keyword string) ([]*entity.DBConnection, int64, error) {
	return s.repo.GetConnections(ctx, page, pageSize, keyword)
}

// GetConnection 获取单个数据库连接
func (s *DBServiceImpl) GetConnection(ctx context.Context, id int64) (*entity.DBConnection, error) {
	return s.repo.GetConnection(ctx, id)
}

// UpdateConnection 更新数据库连接
func (s *DBServiceImpl) UpdateConnection(ctx context.Context, conn *entity.DBConnection) error {
	return s.repo.UpdateConnection(ctx, conn)
}

// DeleteConnection 删除数据库连接
func (s *DBServiceImpl) DeleteConnection(ctx context.Context, id int64) error {
	return s.repo.DeleteConnection(ctx, id)
}

// TestConnection 测试数据库连接
func (s *DBServiceImpl) TestConnection(ctx context.Context, conn *entity.DBConnection) error {
	return s.repo.TestConnection(ctx, conn)
}

// GetTables 获取表列表
func (s *DBServiceImpl) GetTables(ctx context.Context, id int64) ([]*entity.TableInfo, error) {
	return s.repo.GetTables(ctx, id)
}

// GetTableInfo 获取表信息
func (s *DBServiceImpl) GetTableInfo(ctx context.Context, id int64, tableName string) (*entity.TableInfo, error) {
	return s.repo.GetTableInfo(ctx, id, tableName)
}

// GetColumns 获取列信息
func (s *DBServiceImpl) GetColumns(ctx context.Context, id int64, tableName string) ([]*entity.ColumnInfo, error) {
	return s.repo.GetColumns(ctx, id, tableName)
}

// GetTableData 获取表数据
func (s *DBServiceImpl) GetTableData(ctx context.Context, id int64, tableName string, page, pageSize int) (*entity.QueryResult, error) {
	return s.repo.GetTableData(ctx, id, tableName, page, pageSize)
}

// ExecuteQuery 执行查询
func (s *DBServiceImpl) ExecuteQuery(ctx context.Context, id int64, query string) (*entity.QueryResult, error) {
	return s.repo.ExecuteQuery(ctx, id, query)
}

// ExecuteTransaction 执行事务
func (s *DBServiceImpl) ExecuteTransaction(ctx context.Context, id int64, queries []string) error {
	return s.repo.ExecuteTransaction(ctx, id, queries)
}

// ExecuteBatch 执行批量操作
func (s *DBServiceImpl) ExecuteBatch(ctx context.Context, id int64, query string, params [][]interface{}) error {
	return s.repo.ExecuteBatch(ctx, id, query, params)
}

// CreateDatabase 创建数据库
func (s *DBServiceImpl) CreateDatabase(ctx context.Context, id int64, name string) error {
	return s.repo.CreateDatabase(ctx, id, name)
}

// DropDatabase 删除数据库
func (s *DBServiceImpl) DropDatabase(ctx context.Context, id int64, name string) error {
	return s.repo.DropDatabase(ctx, id, name)
}

// CreateTable 创建表
func (s *DBServiceImpl) CreateTable(ctx context.Context, id int64, tableName string, columns []*entity.ColumnInfo) error {
	return s.repo.CreateTable(ctx, id, tableName, columns)
}

// DropTable 删除表
func (s *DBServiceImpl) DropTable(ctx context.Context, id int64, tableName string) error {
	return s.repo.DropTable(ctx, id, tableName)
}

// AlterTable 修改表
func (s *DBServiceImpl) AlterTable(ctx context.Context, id int64, tableName string, changes []string) error {
	return s.repo.AlterTable(ctx, id, tableName, changes)
}

// GetDatabaseStats 获取数据库统计信息
func (s *DBServiceImpl) GetDatabaseStats(ctx context.Context, id int64) (*entity.DatabaseStats, error) {
	return s.repo.GetDatabaseStats(ctx, id)
}

// GetTableStats 获取表统计信息
func (s *DBServiceImpl) GetTableStats(ctx context.Context, id int64, tableName string) (*entity.TableStats, error) {
	return s.repo.GetTableStats(ctx, id, tableName)
}

// GetQueryStats 获取查询统计信息
func (s *DBServiceImpl) GetQueryStats(ctx context.Context, id int64) ([]*entity.QueryStats, error) {
	return s.repo.GetQueryStats(ctx, id)
}

// GetConnectionStats 获取连接统计信息
func (s *DBServiceImpl) GetConnectionStats(ctx context.Context, id int64) (*entity.ConnectionStats, error) {
	return s.repo.GetConnectionStats(ctx, id)
}
